<?php
/*
 * File index.php for project Rijlesplanner.nl
 * Created on 19 nov 2007 at 21:27:21 by Ivar Pruijn
 */
 
error_reporting(E_ALL);
ini_set("soap.wsdl_cache_ttl", 60);
 
set_include_path(
    get_include_path() . PATH_SEPARATOR .
    dirname(__FILE__) . DIRECTORY_SEPARATOR
);
 
require_once ("includes/header.inc.php");
/* Set locale to Dutch */
setlocale(LC_ALL, 'nl_NL');

require_once("includes/authentication.inc.php");

$module = isset($_GET['module']) ? $_GET['module'] : "home";
$module = isset($_POST['module']) ? $_POST['module'] : $module;
$option = isset($_GET['option']) ? $_GET['option'] : "view";
$option = isset($_POST['option']) ? $_POST['option'] : $option;
$action = isset($_GET['action']) ? $_GET['action'] : "";
$action = isset($_POST['action']) ? $_POST['action'] : $action;

$pageTitle = "";
$wsclient = null;
$wsclient = new SoapClient($config['dashboard_wsdl'], array(
	'trace'=>1,
	'encoding' => 'ISO-8859-1',
	'classmap' => 
		array(
			'Adres'=>"Adres",
			'AgendaBlok'=>"AgendaBlok",
			'AgendaItem'=>"AgendaItem",
			'AgendaItemType'=>"AgendaItemType",
			'AgendaTheorie'=>"AgendaTheorie",
			'Cursus'=>"Cursus",
			'CursusStatistiek'=>"CursusStatistiek",
			'Factuur'=>"Factuur",
			'FactuurRegel'=>"FactuurRegel",
			'Instructeur'=>"Instructeur",
			'Leerling' => "Leerling",
			'LeerlingReactie'=>"LeerlingReactie",
			'Object'=>"Object",
			'Transactie'=>"Transactie",
			'VekaTheorieCursus'=>"VekaTheorieCursus",
			'VekaTheorieDetails'=>"VekaTheorieDetails",
			'VekaTheorieModule'=>"VekaTheorieModule",
			'VekaTheorieZwakkePunten'=>"VekaTheorieZwakkePunten",
			'VorderingFase'=>"VorderingFase",
			'VorderingKaart'=>"VorderingKaart",
			'VorderingNotitie'=>"VorderingNotitie",
			'VorderingRegel'=>"VorderingRegel",
			'VorderingResultaat'=>"VorderingResultaat",
		)
	)
);

$_SESSION['wsclient'] = $wsclient;

if (!isset($_SESSION['NietAkkoord'])) {
	$akkoordEigenVerklaring = false;
	$algemeneVoorwaardenVerplicht = false;
} else {
	$akkoordEigenVerklaring = in_array("eigen_verklaring",$_SESSION['NietAkkoord']) ? false : true; 
	$algemeneVoorwaardenVerplicht = in_array("algemene_voorwaarden_verplicht",$_SESSION['NietAkkoord']) ? true : false;
}

$smarty->assign('akkoordEigenVerklaring',$akkoordEigenVerklaring);
$smarty->assign('algemeneVoorwaardenVerplicht',$algemeneVoorwaardenVerplicht);

# Een leerling moet akkoord gaan met de Algemene voorwaarden voordat het systeem gebruikt kan worden
if($algemeneVoorwaardenVerplicht && $_SESSION['user']->getUserType() == $_SESSION['config']['leerling_usertype']) {
	// redirect to akkoord page
	if(isset($_GET['module']) && isset($_GET['option'])) {
		if ($_GET['module'] == 'home' && $_GET['option'] == 'algemenevoorwaardendownloaden') {
			# Algemene voorwaarden downloaden -> doe niets
		} else if ($_GET['module'] == 'home' && $_GET['option'] == 'algemenevoorwaarden') {
			# Algemene voorwaarden  -> doe niets
		} else {
			header("Location: /home/algemenevoorwaarden/");
		}
  }
} else {
	$akkoordAlgemeneVoorwaarden = true;
}

$referer = $auth->determineReferer("");
$smarty->assign('referer',$_SESSION['config']['siteurl']);

$logger->log("Current module is ".$module."...",PEAR_LOG_DEBUG);
if (!$auth->accessTo($module)) {
	$err = array("desc"=>"Access to module '".$module."' denied");
} else {
	if (file_exists("modules/" . $module . ".php")) {
		include ($config['module_dir'] . "/" . $module . ".php");
		if (!isset ($mobObj) || !(is_object($modObj)) || ($modObj->name != $module)) {
			if (isset ($modObj) && is_object($modObj)) {
				// first close it neatly
				$modObj . stop();
			}
			// new module should be instantiated to overwrite the old module object
			$modObj = new $module();
			$modObj->configure(array (
				"config" => $config,
				"logger" => $logger,
				"user" => $user,
				"wsclient" => $_SESSION['wsclient'],
				"auth" => $auth
			));
		}
		$logger->log("Processing module ".$modObj."...",PEAR_LOG_DEBUG);
		$modObj->process($smarty);
		$pageTitle = $config['default_title']." - " . $modObj->getTitle();
		$subtitle = $modObj->getSubTitle();
		$pageTitle .= !empty ($subtitle) ? ": " . $subtitle : "";
	} else {
		$logger->log( "Module '" . $module . "' not found",PEAR_LOG_ERR);
	}
}

// check whether the page is requested via a modal popup window or normally
if ((isset($_GET['action']) && $_GET['action'] == "modal") || (isset($_GET['method']) && $_GET['method'] == "modal") || (isset($_POST['method']) && $_POST['method'] == "modal")) {
	$template = "modals.tpl";
	// add "_modal.tpl" to the assigned module template
	$mt = $smarty->get_template_vars('moduleTemplate');
	$mt2 = substr($mt,0,-4);
	$mt3 = $mt2."_modal.tpl";
	$smarty->assign('moduleTemplate',$mt3);
} else {
	$template = "index.tpl";
}

// Set the correct layout template
// Is LayoutStyle defined -> if not set default
if(!isset($_SESSION['LayoutStyle']) || $_SESSION['LayoutStyle'] == '') {
	$_SESSION['LayoutStyle'] = 'default';
}

// Determine the paths
$imagesURL =  $config['siteurl'] . '/images/' . $_SESSION['LayoutStyle'];
$cssURL = $config['siteurl'] . '/style/' . $_SESSION['LayoutStyle'] . '.css';

// Assign the smarty variables
$smarty->assign('imagesURL',$imagesURL);
$smarty->assign('cssURL',$cssURL);
if (isset($_SESSION['config'])) {
	$smarty->assign('default_leerling_photo', $imagesURL.'/'.$_SESSION['config']['default_leerling_photo']);
}

# Set the handle to nothing if there is no handle
if (isset($_SESSION['redirect_rijschool'])) {
	unset($_SESSION['redirect_rijschool']);
	$smarty->assign('handle_redirect_show',true);
}

if(!isset($_SESSION['redirect_rijschool_handle'])) {
	$_SESSION['redirect_rijschool_handle'] = "";
}

$smarty->assign('handle_redirect',$_SESSION['redirect_rijschool_handle']);

$metaKeywords = "";
$metaDescription = "";
$metaExtra = array ();
$linkTags = array ();

$smarty->assign('siteURL', $config['siteurl']);
$smarty->assign('pageTitle', $pageTitle);
$smarty->assign('metaKeywords', $metaKeywords);
$smarty->assign('metaDescription', $metaDescription);
$smarty->assign('metaExtra', $metaExtra);
$smarty->assign('linkTags', $linkTags);
$smarty->assign('PHP_SELF',$_SERVER['PHP_SELF']);
$smarty->assign('REQUEST_URI',htmlspecialchars(str_replace('#^//#', '/', $_SERVER['REQUEST_URI'])));

$smarty->assign('module', $module);
$smarty->assign('option', $option);

$smarty->display($template);

?>
